package com.tian.arch.widget

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.Gravity
import androidx.appcompat.widget.AppCompatButton
import com.tian.arch.R
import com.tian.arch.util.*


/**
 *  灵活多变的Button，ui标准下定义下的按钮，使用请参见[ArchTestAc]
 *
 *  ```
 *  1、拥有2种模式，单色或渐变模式
 *  app:isGradient="true or false"
 *
 *  2、支持自定义
 *
 *  app:isSupportUserDefine="true"      // 开自定义开关
 *
 *  app:wsCornerRadius="radius8"        // 圆角弧度
 *  app:normalColor="@color/accent"     // 正常状态下 颜色或渐变启点色
 *  app:pressedColor="@color/red"       // 按下状态下 颜色或渐变启点色
 *  app:normalEndColor="@color/blue"    // 正常状态下渐变end色
 *  app:pressedEndColor="..."           // 按下状态下渐变end色
 *
 *  app:unEnableColor=".."              // 按钮unEnable下的填充色
 *
 *  自定义情况下可以 更改文本字体大小，padding间距
 *
 *  3、默认情况下(在非自定义模式下，app:isSupportUserDefine="false" 或者不定义此属性)
 *  按钮文本字号是16sp
 *  内部padding,top:16dp,bottom:16dp,left:32dp,right:32dp
 *  默认情况下。不能更改 文本字体大小，padding间距
 *  ```
 */
class FickleButton @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : AppCompatButton(context, attrs, defStyleAttr) {

    init {
        initSelf(context.obtainStyledAttributes(attrs, R.styleable.FickleButton, defStyleAttr, 0))
    }

    private fun initSelf(ta: TypedArray) {
        gravity = Gravity.CENTER
        val isSupportUserDefine = ta.getBoolean(R.styleable.FickleButton_isSupportUserDefine, false)
        val isGradient = ta.getBoolean(R.styleable.FickleButton_isGradient, false)
        val unEnableTextColor = ta.getColor(R.styleable.FickleButton_unEnableTextColor, 0)
        val normalTextColor = ta.getColor(R.styleable.FickleButton_normalTextColor, 0)

        if (!isSupportUserDefine) { // 不支持自定义属性
            textSize = 16f
            setPadding(32.dp().toInt(), 16.dp().toInt(), 32.dp().toInt(), 16.dp().toInt())
            background = if (isGradient) {
                createGradientButtonSelector(context)
            } else {
                createNormalButtonSelector(context)
            }
        } else {
            val normalColor = ta.getColor(R.styleable.FickleButton_normalColor, 0)
            val normalEndColor = ta.getColor(R.styleable.FickleButton_normalEndColor, 0)
            val strokeColor = ta.getColor(R.styleable.FickleButton_strokeColor, 0)
            val strokeWidthIndex = ta.getInt(R.styleable.FickleButton_strokeColorWidth, -1)

            val pressedColor = ta.getColor(R.styleable.FickleButton_pressedColor, 0)
            val pressedEndColor = ta.getColor(R.styleable.FickleButton_pressedEndColor, 0)

            val unEnableColor = ta.getColor(R.styleable.FickleButton_unEnableColor, 0)
            val unEnableEndColor = ta.getColor(R.styleable.FickleButton_unEnableEndColor, 0)
            val unEnableStrokeColor = ta.getColor(R.styleable.FickleButton_unEnableShapeStrokeColor, 0)
            val unEnableStrokeWidthIndex = ta.getInt(R.styleable.FickleButton_unEnableShapeStrokeWidth, -1)

            val cornerRadiusIndex = ta.getInt(R.styleable.FickleButton_wsCornerRadius, -1)

            val corner = if (cornerRadiusIndex == -1) ShapeCornerRadius.Radius24 else ShapeCornerRadius.values()[cornerRadiusIndex]
            val unEnableStrokeWidth = if (unEnableStrokeWidthIndex == -1) null else ShapeStokeWidth.values()[unEnableStrokeWidthIndex]
            val strokeWidth = if (strokeWidthIndex == -1) null else ShapeStokeWidth.values()[strokeWidthIndex]

            background = if (!isGradient) {
                createNormalButtonSelector(
                    context,
                    normalColor,
                    corner,
                    pressedColor,
                    unEnableColor,
                    strokeColor = strokeColor,
                    strokeWidth = strokeWidth,
                    unEnableStrokeColor = unEnableStrokeColor,
                    unEnableStrokeWidth = unEnableStrokeWidth
                )
            } else {
                createGradientButtonSelector(
                    context,
                    normalColor, normalEndColor, pressedColor, pressedEndColor, unEnableColor, unEnableEndColor, corner,
                    unEnableStrokeColor = unEnableStrokeColor, unEnableStrokeWidth = unEnableStrokeWidth
                )
            }
        }

        setTextColor(
            createTextColorSelector(
                if (unEnableTextColor == 0) R.color.cor_cad4.toResColor(context) else unEnableTextColor,
                if (normalTextColor == 0) R.color.white.toResColor(context) else normalTextColor,
                SelectorState.Enabled
            )
        )

        ta.recycle()
    }
}